package com.xydtech.ecard.ecard.service.impl;

import com.xydtech.common.utils.AESEncryptionUtils;
import com.xydtech.common.utils.StringUtils;
import com.xydtech.ecard.ecard.domain.EcardApiMerchantPartInfo;
import com.xydtech.ecard.ecard.mapper.EcardApiMerchantPartInfoMapper;
import com.xydtech.ecard.ecard.service.IEcardApiMerchantPartInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 电子名片商户成员Service业务层处理
 *
 * @author xydtech
 * @date 2023-07-25
 */
@Service
public class EcardApiMerchantPartInfoServiceImpl implements IEcardApiMerchantPartInfoService {
    @Autowired
    private EcardApiMerchantPartInfoMapper ecardMerchantPartInfoMapper;

    /**
     * 查询电子名片个人/商户/企业/新型农业主体成员
     *
     * @param id 电子名片商户成员主键
     * @return 电子名片商户成员
     */
    @Override
    public EcardApiMerchantPartInfo selectEcardMerchantPartInfoById(Long id, String salt) {
        EcardApiMerchantPartInfo merchantPartInfo = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoById(id);
        if (merchantPartInfo != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                    merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                    merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return merchantPartInfo;
    }

    @Override
    public EcardApiMerchantPartInfo selectEcardMerchantPartInfoByIdAndCreatordId(Long id, Long creatorId, String salt) {
        EcardApiMerchantPartInfo merchantPartInfo = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoByIdAndCreatorId(id, creatorId);
        if (merchantPartInfo != null) {
            try {
                //数据解密（手机号、身份证号）
                if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                    merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                }
                if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                    merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
        return merchantPartInfo;
    }

    /**
     * 查询电子名片个人/商户/企业/新型农业主体成员列表
     *
     * @param merchantId 电子名片商户ID
     * @return 电子名片商户成员
     */
    @Override
    public List<EcardApiMerchantPartInfo> getMerchantPartList(Long merchantId, String merchantType, Long deptId, String salt) {
        List<EcardApiMerchantPartInfo> merchantPartList = ecardMerchantPartInfoMapper.getMerchantPartList(merchantId, merchantType, deptId);
        if (merchantPartList.size() > 0) {
            for (EcardApiMerchantPartInfo merchantPartInfo : merchantPartList) {
                try {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                        merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                        merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        }
        return merchantPartList;
    }

    /**
     * 新增电子名片个人/商户/企业/新型农业主体成员
     *
     * @param ecardMerchantPartInfo 电子名片商户成员
     * @return 结果
     */
    @Override
    public int addMerchantPartInfo(EcardApiMerchantPartInfo ecardMerchantPartInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getPhone())) {
                ecardMerchantPartInfo.setPhone(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getIdCard())) {
                ecardMerchantPartInfo.setIdCard(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getIdCard(), salt));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return ecardMerchantPartInfoMapper.addMerchantPartInfo(ecardMerchantPartInfo);
    }

    /**
     * 查询电子名片个人/商户/企业/新型农业主体成员列表
     * @param ecardMerchantPartInfo 电子名片商户成员
     * @return
     */
    @Override
    public List<EcardApiMerchantPartInfo> selectEcardMerchantPartInfoList(EcardApiMerchantPartInfo ecardMerchantPartInfo, String salt) {
        try {
            //数据加密（手机号、身份证号）
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getPhone())) {
                ecardMerchantPartInfo.setPhone(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getPhone(), salt));
            }
            if (!StringUtils.objIsNullOrEmpty(ecardMerchantPartInfo.getIdCard())) {
                ecardMerchantPartInfo.setIdCard(AESEncryptionUtils.encrypt(ecardMerchantPartInfo.getIdCard(), salt));
            }
            List<EcardApiMerchantPartInfo> merchantPartList = ecardMerchantPartInfoMapper.selectEcardMerchantPartInfoList(ecardMerchantPartInfo);
            if (merchantPartList.size() > 0) {
                for (EcardApiMerchantPartInfo merchantPartInfo : merchantPartList) {
                    //数据解密（手机号、身份证号）
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getPhone())) {
                        merchantPartInfo.setPhone(AESEncryptionUtils.decrypt(merchantPartInfo.getPhone(), salt));
                    }
                    if (!StringUtils.objIsNullOrEmpty(merchantPartInfo.getIdCard())) {
                        merchantPartInfo.setIdCard(AESEncryptionUtils.decrypt(merchantPartInfo.getIdCard(), salt));
                    }
                }
            }
            return merchantPartList;
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    /**
     * 修改个人/商户/企业/新型农业主体成员联系人接口
     *
     * @author yjs
     * @date 2023/9/13 14:22
     */
    @Override
    public int modifyMerchantContacts(Long id, Long merchantId, String merchantType) {
        return ecardMerchantPartInfoMapper.modifyMerchantContacts(id, merchantId, merchantType);
    }

    /**
     * 删除电子名片个人/商户/企业/新型农业主体成员信息
     *
     * @param id 电子名片商户成员主键
     * @return 结果
     */
    @Override
    public int deleteEcardMerchantPartInfoById(Long id) {
        return ecardMerchantPartInfoMapper.deleteEcardMerchantPartInfoById(id);
    }
}
